import os
from PyQt5.QtWidgets import (QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QLabel, 
                             QFileDialog, QComboBox, QCheckBox, QSpinBox, QLineEdit, 
                             QGroupBox)
from PyQt5.QtCore import Qt
import multiprocessing

def create_settings_page(main_window):
    """创建设置页面"""
    settings_widget = QWidget()
    settings_widget.setObjectName("tab_content")  # 添加对象名，便于样式应用
    settings_layout = QVBoxLayout(settings_widget)
    settings_layout.setSpacing(15)  # 增加间距
    settings_layout.setContentsMargins(20, 20, 20, 20)  # 增加边距
    
    # FFmpeg路径设置
    ffmpeg_group = QGroupBox("FFmpeg设置")
    ffmpeg_layout = QVBoxLayout(ffmpeg_group)
    ffmpeg_layout.setSpacing(10)
    ffmpeg_layout.setContentsMargins(15, 15, 15, 15)  # 增加内边距
    
    ffmpeg_path_layout = QHBoxLayout()
    ffmpeg_path_label = QLabel("FFmpeg路径:")
    ffmpeg_path_label.setMinimumWidth(80)
    ffmpeg_path_label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
    main_window.ffmpeg_path_edit = QLineEdit()
    main_window.ffmpeg_path_edit.setReadOnly(True)
    ffmpeg_path_btn = QPushButton("浏览...")
    ffmpeg_path_btn.setFixedWidth(80)
    ffmpeg_path_btn.clicked.connect(lambda: select_ffmpeg_path(main_window))
    
    ffmpeg_path_layout.addWidget(ffmpeg_path_label)
    ffmpeg_path_layout.addWidget(main_window.ffmpeg_path_edit)
    ffmpeg_path_layout.addWidget(ffmpeg_path_btn)
    
    ffmpeg_accel_layout = QHBoxLayout()
    ffmpeg_accel_label = QLabel("FFmpeg硬件加速:")
    ffmpeg_accel_label.setMinimumWidth(80)
    ffmpeg_accel_label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
    main_window.ffmpeg_accel_combo = QComboBox()
    main_window.ffmpeg_accel_combo.addItem("cpu")
    
    ffmpeg_accel_layout.addWidget(ffmpeg_accel_label)
    ffmpeg_accel_layout.addWidget(main_window.ffmpeg_accel_combo)
    ffmpeg_accel_layout.addStretch()
    
    ffmpeg_layout.addLayout(ffmpeg_path_layout)
    ffmpeg_layout.addLayout(ffmpeg_accel_layout)
    
    # MiDaS设置
    midas_group = QGroupBox("MiDaS设置")
    midas_layout = QVBoxLayout(midas_group)
    midas_layout.setSpacing(10)
    midas_layout.setContentsMargins(15, 15, 15, 15)  # 增加内边距
    
    midas_accel_layout = QHBoxLayout()
    midas_accel_label = QLabel("MiDaS硬件加速:")
    midas_accel_label.setMinimumWidth(80)
    midas_accel_label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
    main_window.midas_accel_combo = QComboBox()
    main_window.midas_accel_combo.addItem("cpu")
    
    midas_accel_layout.addWidget(midas_accel_label)
    midas_accel_layout.addWidget(main_window.midas_accel_combo)
    midas_accel_layout.addStretch()
    
    midas_layout.addLayout(midas_accel_layout)
    
    # 添加立体视图生成设置
    stereo_group = QGroupBox("立体视图生成设置")
    stereo_layout = QVBoxLayout(stereo_group)
    stereo_layout.setSpacing(10)
    stereo_layout.setContentsMargins(15, 15, 15, 15)  # 增加内边距
    
    stereo_accel_layout = QHBoxLayout()
    stereo_accel_label = QLabel("硬件加速:")
    stereo_accel_label.setMinimumWidth(80)
    stereo_accel_label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
    main_window.stereo_accel_combo = QComboBox()
    main_window.stereo_accel_combo.addItem("cpu (CPU处理)")
    main_window.stereo_accel_combo.addItem("cuda (NVIDIA GPU加速)")
    main_window.stereo_accel_combo.addItem("opencl (OpenCL加速)")
    
    stereo_accel_layout.addWidget(stereo_accel_label)
    stereo_accel_layout.addWidget(main_window.stereo_accel_combo)
    stereo_accel_layout.addStretch()
    
    # 并行处理选项
    parallel_layout = QHBoxLayout()
    parallel_label = QLabel("")
    parallel_label.setMinimumWidth(80)
    parallel_label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
    main_window.parallel_check = QCheckBox("启用并行处理")
    main_window.parallel_check.setChecked(True)
    
    parallel_layout.addWidget(parallel_label)
    parallel_layout.addWidget(main_window.parallel_check)
    parallel_layout.addStretch()
    
    threads_layout = QHBoxLayout()
    threads_label = QLabel("线程数:")
    threads_label.setMinimumWidth(80)
    threads_label.setAlignment(Qt.AlignRight | Qt.AlignVCenter)
    main_window.threads_spin = QSpinBox()
    main_window.threads_spin.setMinimum(1)
    main_window.threads_spin.setMaximum(multiprocessing.cpu_count())
    main_window.threads_spin.setValue(max(1, multiprocessing.cpu_count() - 1))
    
    threads_layout.addWidget(threads_label)
    threads_layout.addWidget(main_window.threads_spin)
    threads_layout.addStretch()
    
    stereo_layout.addLayout(stereo_accel_layout)
    stereo_layout.addLayout(parallel_layout)
    stereo_layout.addLayout(threads_layout)
    
    # 添加到设置页面
    settings_layout.addWidget(ffmpeg_group)
    settings_layout.addWidget(midas_group)
    settings_layout.addWidget(stereo_group)
    settings_layout.addStretch()
    
    # 添加到标签页
    main_window.content_stack.addTab(settings_widget, "初始设置")
    
    # 移除这行，因为它会覆盖全局样式
    # settings_widget.setStyleSheet("background-color: transparent;")
    
    return settings_widget

def select_ffmpeg_path(main_window):
    """选择FFmpeg路径"""
    file_dialog = QFileDialog(main_window)
    file_dialog.setWindowTitle("选择FFmpeg可执行文件")
    file_dialog.setNameFilter("可执行文件 (*.exe)")
    file_dialog.setFileMode(QFileDialog.ExistingFile)
    
    if file_dialog.exec_():
        selected_files = file_dialog.selectedFiles()
        if selected_files:
            ffmpeg_path = selected_files[0]
            main_window.ffmpeg_path = ffmpeg_path
            main_window.ffmpeg_path_edit.setText(ffmpeg_path)
            main_window.append_log(f"已设置FFmpeg路径: {ffmpeg_path}")
            
            # 启用视频处理和左右眼合并按钮
            if hasattr(main_window, 'process_btn'):
                main_window.process_btn.setEnabled(True)
            if hasattr(main_window, 'merge_btn'):
                main_window.merge_btn.setEnabled(True)
            if hasattr(main_window, 'crop_btn'):
                main_window.crop_btn.setEnabled(True)
            # 更新硬件检测线程中的FFmpeg路径
            if hasattr(main_window, 'hardware_thread'):
                main_window.hardware_thread.set_ffmpeg_path(ffmpeg_path)
                main_window.hardware_thread.start()